﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.LIB;
using YJKRevitKernel.FormUtility;
using YJKCopyModel.InteractiveOperation;
using YJKCopyModel.RoomTransModel;

namespace YJKCopyModel.Forms.RoomTransModelForms
{
	public partial class StatisticsForm : System.Windows.Forms.Form
	{
		public Dictionary<RoomConfigInfo, List<TextInfo>> ConfigAndText { get; set; }

		public StatisticsForm(Autodesk.Revit.DB.Document doc, Dictionary<RoomConfigInfo, List<TextInfo>> dicConfigAndText)
		{
			this.InitializeComponent();
			this.m_ctrlManager = new ControlInfoManager(this, new List<KeyValuePair<System.Windows.Forms.Control, bool>>(), null, null);
			this.m_doc = doc;
			bool flag = !dicConfigAndText.IsEmpty<KeyValuePair<RoomConfigInfo, List<TextInfo>>>();
			if (flag)
			{
				this.m_dicConfigAndText = (from item in dicConfigAndText
				orderby item.Key.RoomName
				select item).ToDictionary((KeyValuePair<RoomConfigInfo, List<TextInfo>> item) => item.Key, (KeyValuePair<RoomConfigInfo, List<TextInfo>> item) => item.Value);
			}
		}

		private void StatisticsForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.SetControlInfoToForm();
			}
			this.InitGridView();
			this.InitCombox();
		}

		private void InitGridView()
		{
			this.dgvRoomName.Rows.Clear();
			int num = Math.Max(this.m_dicConfigAndText.Count, 20);
			this.dgvRoomName.Rows.Add(num);
			for (int i = 0; i < num; i++)
			{
				DataGridViewRow dataGridViewRow = this.dgvRoomName.Rows[i];
				bool flag = i < this.m_dicConfigAndText.Count;
				if (flag)
				{
					dataGridViewRow.Cells[0].Value = true;
					dataGridViewRow.Cells[1].Value = this.m_dicConfigAndText.ElementAt(i).Key.RoomName;
					dataGridViewRow.Cells[2].Value = this.m_dicConfigAndText.ElementAt(i).Value.Count;
				}
				else
				{
					dataGridViewRow.Cells[0].Value = null;
					dataGridViewRow.ReadOnly = true;
				}
			}
		}

		private void InitCombox()
		{
			List<RoomTagType> roomTagTypeInDoc = RoomManager.GetRoomTagTypeInDoc(this.m_doc);
			List<string> list = new List<string>();
			foreach (RoomTagType type in roomTagTypeInDoc)
			{
				string item = RoomManager.FormatRoomTagTypeName(type);
				list.Add(item);
			}
			list.Sort();
			this.cmbSymbol.Items.Add("<程序自选>");
			this.cmbSymbol.Items.AddRange(list.ToArray());
			this.cmbSymbol.SelectedIndex = 0;
		}

		private void btnTransform_Click(object sender, EventArgs e)
		{
			this.UpdateCongfigInfo();
			base.DialogResult = DialogResult.OK;
		}

		private void UpdateCongfigInfo()
		{
			Dictionary<RoomConfigInfo, List<TextInfo>> dictionary = new Dictionary<RoomConfigInfo, List<TextInfo>>();
			string text = this.cmbSymbol.Text;
			for (int i = 0; i < this.m_dicConfigAndText.Count; i++)
			{
				RoomConfigInfo roomConfigInfo = new RoomConfigInfo();
				DataGridViewRow dataGridViewRow = this.dgvRoomName.Rows[i];
				roomConfigInfo.Rebuild = bool.Parse(dataGridViewRow.Cells[0].Value.ToString());
				roomConfigInfo.RoomName = dataGridViewRow.Cells[1].Value.ToString();
				roomConfigInfo.RoomTypeName = text;
				dictionary.Add(roomConfigInfo, this.m_dicConfigAndText.ElementAt(i).Value);
			}
			this.ConfigAndText = dictionary;
		}

		private void butCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void StatisticsForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.GetControlInfoToXml();
			}
		}

		private const int c_nGridViewRowMinCount = 20;

		private const string c_strDefaultItem = "<程序自选>";

		private Document m_doc = null;

		private Dictionary<RoomConfigInfo, List<TextInfo>> m_dicConfigAndText = new Dictionary<RoomConfigInfo, List<TextInfo>>();

		private ControlInfoManager m_ctrlManager = null;
	}
}
